<template>
    <div class="chart-wrapper">
        <h3 class="chart-title">历年豆类、谷类、薯类产量排名</h3>
        <div ref="myEcharts" style="width:100%;height:360px"></div>
    </div>
</template>

<script setup>
    import * as echarts from 'echarts'
    import { ref,onMounted,reactive } from 'vue';
    import axios from 'axios';
    let options = reactive([])
    const myEcharts = ref();
    function echartsInit(){
        const mc = echarts.init(myEcharts.value, {
            backgroundColor: 'transparent'
        })
        var option = {
            backgroundColor: 'transparent',
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            legend: {
                textStyle:{
                    color:'white'
                }
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                top: '15%',
                containLabel: true
            },
            xAxis: [
                {
                type: 'category',
                data: ['2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020','2021','2022']
                }
            ],
            yAxis: [
                {
                type: 'value'
                }
            ],
            series: [
                {
                name: '谷类',
                type: 'bar',
                itemStyle: {
                    color: '#74FFFF'
                },
                emphasis: {
                    focus: 'series'
                },
                data: []
                },
                {
                name: '豆类',
                type: 'bar',
                itemStyle: {
                    color: 'yellow'
                },
                emphasis: {
                    focus: 'series'
                },
                data: []
                },
                {
                name: '薯类',
                type: 'bar',
                itemStyle: {
                    color: 'pink'
                },
                emphasis: {
                    focus: 'series'
                },
                data: []
                },
            ]
        };
        const xdata = reactive([]) 
        const ydata = reactive([]) 
        const zdata = reactive([])
        for(let index=0;index<options.length;index++){
            let obj = options[index];
            xdata.push(obj.cerealyield);
            ydata.push(obj.legumesyield);
            zdata.push(obj.potatoproduction)
        }
        option.series[0].data = xdata
        option.series[1].data = ydata
        option.series[2].data = zdata
        mc.setOption(option);
    }
    function getData(){
        axios.get('/api/queryNagrProp')
            .then((response)=>{
                if(response.data.code==200){
                    options = response.data.data;
                    echartsInit();
                }
            })
    }
    onMounted(()=>{
        getData();
    })
     
</script>

<style scoped>
.chart-wrapper {
    width: 100%;
    height: 100%;
    padding: 10px;
    background: transparent;
}

.chart-title {
    color: #fff;
    margin: 0;
    font-size: 18px;
    margin-bottom: 10px;
    text-align: left;
    padding-left: 20px;
}
</style>
